package org.kalai.unifypay.wft;

import java.io.IOException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Date;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.kalai.unifypay.Constant;
import org.kalai.unifypay.util.CommonUtil;



public class WFTPay {

    private static final Logger logger = Logger.getLogger(WFTPay.class);

    /**
     * 威富通扫码支付
     * @param tranxSN
     * @param cent
     * @param subject
     * @return
     */
    public static Map<String, Object> codePay(String tranxSN, long cent, String subject) {

        Map<String, Object> respMap = CommonUtil.createRespMap("XXXX", "验签失败");

        SortedMap<String, String> map = new TreeMap<String, String>();
        map.put("service", "pay.weixin.native");
        map.put("version", "2.0");
        map.put("charset", "UTF-8");
        map.put("sign_type", "MD5");
        map.put("out_trade_no", tranxSN);
        map.put("body", subject);
        map.put("total_fee", Long.toString(cent));
        map.put("mch_create_ip", "127.0.0.1");
        map.put("mch_id", Constant.WFT_MCH_ID);
        map.put("notify_url", Constant.WFT_NOTIFY_URL);
        map.put("nonce_str", String.valueOf(new Date().getTime()));

        Map<String, String> param = WFTUtil.paraFilter(map);

        StringBuilder sbd = new StringBuilder((param.size() + 1) * 10);
        WFTUtil.buildPayParams(sbd, param, false);
        String preStr = sbd.toString();
        String sign = WFTUtil.sign(preStr, "&key=" + Constant.WFT_KEY, "utf-8");

        map.put("sign", sign);

        String reqUrl = Constant.WFT_REQ_URL;

        Map<String, String> resultMap = WFTUtil.sendPost(reqUrl, map);

        if(resultMap.containsKey("sign")) {
            if(!WFTUtil.checkParam(resultMap, Constant.WFT_KEY)) {
                logger.error("验证签名不通过!");
            } else {
                if ("0".equals(resultMap.get("status")) && "0".equals(resultMap.get("result_code"))) {
                    respMap = CommonUtil.createRespMap("00", "威富通扫码支付");
                    respMap.put("codeUrl", resultMap.get("code_url"));
                } else {
                    respMap.put("respCode", "10");
                    respMap.put("respDesc", resultMap);
                    logger.error(String.format("(威富通)支付失败:[resultMap=%s]", resultMap));
                }
            }
        }

        System.out.println("响应码:   "+respMap.get("respCode"));
        System.out.println("响应信息:   "+respMap.get("respDesc"));
        System.out.println("url:   "+respMap.get("codeUrl"));

        return respMap;

    }

    /**
     * 订单查询
     * @param tranxSN
     * @return
     */
    public static Map<String, Object> query(String tranxSN) {

        Map<String, Object> respMap = CommonUtil.createRespMap("XXXX", "验签失败");

        SortedMap<String, String> map = new TreeMap<String, String>();
        map.put("service", "trade.single.query");
        map.put("version", "2.0");
        map.put("charset", "UTF-8");
        map.put("sign_type", "MD5");
        map.put("out_trade_no", tranxSN);
        map.put("mch_id", Constant.WFT_MCH_ID);
        map.put("nonce_str", String.valueOf(new Date().getTime()));

        Map<String, String> param = WFTUtil.paraFilter(map);

        StringBuilder sbd = new StringBuilder((param.size() + 1) * 10);
        WFTUtil.buildPayParams(sbd, param, false);
        String preStr = sbd.toString();
        String sign = WFTUtil.sign(preStr, "&key=" + Constant.WFT_KEY, "utf-8");

        map.put("sign", sign);

        String reqUrl = Constant.WFT_REQ_URL;

        Map<String, String> resultMap = WFTUtil.sendPost(reqUrl, map);

        if(resultMap.containsKey("sign")) {
            if(!WFTUtil.checkParam(resultMap, Constant.WFT_KEY)) {
                logger.error("验证签名不通过!");
            } else {
                if ("0".equals(resultMap.get("status")) && "0".equals(resultMap.get("result_code"))) {
                    respMap = CommonUtil.createRespMap("00", "订单创建成功");
                    respMap.put("state", resultMap.get("trade_state"));
                } else {
                    respMap.put("respCode", "10");
                    respMap.put("respDesc", resultMap);
                    logger.error(String.format("(威富通)支付失败:[resultMap=%s]", resultMap));
                }
            }
        }

        System.out.println("响应码:   "+respMap.get("respCode"));
        System.out.println("响应信息:   "+respMap.get("respDesc"));
        System.out.println("状态:   "+respMap.get("state"));

        return respMap;
    }



    public static void main(String[] args) {

        //codePay("1477106839967", 1, "测试购买商品");
        query("1477106839967");

    }


}
